
package managedbean.CRM.SaleQuotation;

import entity.CRM.LineItem;
import entity.CRM.SaleQuotation;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.CRM.SaleQuotationBeanLocal;

@ManagedBean(name="viewSaleQuotationManagerBean")
@ViewScoped
public class ViewSaleQuotationManagerBean {

    public ViewSaleQuotationManagerBean() {
    }
    @EJB
    private SaleQuotationBeanLocal saleQuotationBeanLocal;
    private SaleQuotation selectedSaleQuotation;
    private List<SaleQuotation> saleQuotations;
    private String statusMessage;
    private List<LineItem> lineItems;
    
    public SaleQuotation getSelectedSaleQuotation(){
        return selectedSaleQuotation;    
    }
    
    public void setSelectedSaleQuotation(SaleQuotation selectedSaleQuotation){
        this.selectedSaleQuotation = selectedSaleQuotation;
    }
    
    public List<SaleQuotation> getSaleQuotations(){
        return saleQuotationBeanLocal.getSaleQuotation();
    }
    
    public List<LineItem> getLineItems(){
        return lineItems;
    }
    
    public void setLineItems(List<LineItem> lineItems){
        this.lineItems = lineItems;
    }

    public void editSaleQuotation(ActionEvent event) throws IOException{
        SaleQuotation selectedQuotation = this.getSelectedSaleQuotation();
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedSaleQuotation", selectedQuotation);
        FacesContext.getCurrentInstance().getExternalContext().redirect("editSaleQuotation.xhtml");
    }
    
    public void deleteSaleQuotation(ActionEvent event) throws IOException{
        try{
            saleQuotationBeanLocal.deleteSaleQuotation(selectedSaleQuotation.getId());
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewSaleQuotation.xhtml");
            statusMessage = "Sale Quotation Deleted Successfully";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void createSaleOrder(ActionEvent event) throws IOException
    {
        SaleQuotation selectedQuotation = this.getSelectedSaleQuotation();
        try
        {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleQuotation", selectedQuotation);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../saleorder/addSaleOrderWithQuotation.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
}
